import { KVAny } from "../type/kv";
import { serialize } from "./serialize";
import { HttpParams, HttpHeaders, HttpClient } from "@angular/common/http";

export enum ParamsType {
  JSON,
  FORM,
  XML,
  UPLOAD,
}

export interface MergedHttpParams {
  url: string;
  params?: any;
  options?: KVAny;
}

export const mergeHttpGetParams = (
  apiUrl: string,
  params: KVAny
): MergedHttpParams => ({
  url: apiUrl,
  params: new HttpParams({
    fromString: serialize(params)
  })
});

// export const mergeHttpPostParamsInJson = (
//   apiUrl,
//   params: KVAny
// ): MergedHttpParams => ({ url: apiUrl, params });
export const mergeHttpPostParamsInJson = (
  apiUrl,
  params: KVAny
): MergedHttpParams => ({
  url: apiUrl,
  params,
  options: {
    headers: new HttpHeaders().set(
      "Content-Type",
      "application/json"
    )
  }
});

export const mergeHttpPostParamsInForm = (
  apiUrl,
  params: KVAny
): MergedHttpParams => ({
  url: apiUrl,
  params: new HttpParams({
    fromString: serialize(params)
  }),
  options: {
    headers: new HttpHeaders().set(
      "Content-Type",
      "application/x-www-form-urlencoded"
    )
  }
});

export const mergeHttpPostParamsInFormUpload = (
  apiUrl,
  params: KVAny
): MergedHttpParams => ({
  url: apiUrl,
  params,
  options: {
    headers: new HttpHeaders().set(
      "Content-Type",
      "application/x-www-form-urlencoded"
    )
  }
});
